import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

import hospital from './hospital';
import management from './management';
// 0--二级医院登录  1--质控中心登录
let loginStyle = sessionStorage.getItem('loginStyle');

const routes = [
  {
    path: '/',
    name: 'LoginPage',
    component: () => import('../views/login.vue'),
    meta: { title: '登录' },
  },
  {
    path: '/index',
    name: 'LoginPage',
    component: () => import('../views/index.vue'),
    redirect:
      loginStyle == 0
        ? '/hospital/userManagement'
        : '/management/userManagement',
    children: [...hospital, ...management],
  },
  {
    path: '*',
    redirect: '/',
  },
];

const router = new VueRouter({
  routes,
});

const originalReplace = VueRouter.prototype.replace;

VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

router.beforeEach((to, form, next) => {
  document.title = to.meta.title || '质控中心'; //修改网页展示title
  next();
});

export default router;
